package com.ming.closeto.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ming.closeto.R;
import com.ming.closeto.model.AppModel;
import com.ming.closeto.utils.GlideUtils;

import java.util.List;

public class AppManagerAdapter extends RecyclerView.Adapter<AppManagerAdapter.ViewHolder> {
    private Context mContext;
    private LayoutInflater inflater;
    private List<AppModel> mList;

    private AppManagerAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(AppManagerAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    // 构造方法
    public AppManagerAdapter(Context mContext, List<AppModel> mList) {
        this.mContext = mContext;
        this.mList = mList;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.layout_app_item, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull AppManagerAdapter.ViewHolder viewHolder, final int position) {
        AppModel model = mList.get(position);
        // 设置文字
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            viewHolder.iv_app_icon.setBackground(model.getIcon());
        } else {
            BitmapDrawable drawable = (BitmapDrawable) model.getIcon();
            Bitmap bitmap = drawable.getBitmap();
            viewHolder.iv_app_icon.setImageBitmap(bitmap);
        }

        viewHolder.tv_app_name.setText(model.getAppName());
        viewHolder.tv_app_packagegname.setText(model.getPackageName());
        viewHolder.tv_app_size.setText(model.getAppSize());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView iv_app_icon;
        private TextView tv_app_name;
        private TextView tv_app_packagegname;
        private TextView tv_app_size;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_app_icon = itemView.findViewById(R.id.iv_app_icon);
            tv_app_name = itemView.findViewById(R.id.tv_app_name);
            tv_app_packagegname = itemView.findViewById(R.id.tv_app_packagename);
            tv_app_size = itemView.findViewById(R.id.tv_app_size);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }
}
